import 'dart:convert';

main(){
  //直接创建对象
  User user=User('张三', 18, '男');
  //打印对象
  print(user.age);
  //后端回来的数据
  String jsonStr='{"name":"张三","age":18,"gender":"男"}';
  //将json字符串转换为Map
  Map jsonMap=json.decode(jsonStr);
  //打印Map
  print(jsonMap);
  print(jsonStr);
  print('用户名时${jsonMap['name']}');
  //将Map转换为对象
  User user2=User.fromJson(jsonMap);
  //打印对象
  print(user2.name);
  print(user2.age);
  print(user2.gender);



}
//1.用户信息
class User{
late String name;
late int age;
late String gender;
//2.构造函数
User(this.name,this.age,this.gender);
//3.命名构造函数 传入Map
User.fromJson(Map jaonMap){
  name = jaonMap['name'];
  age = jaonMap['age'];
  gender = jaonMap['gender'];
}
}